
//  Created by 莫瑞权 on 2022/5/6.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit

class TFQskQuestions1to16Cell: UITableViewCell {
    //提问人的头像
    @objc public let questionerAvatar = UIImageView()
    @objc public let underline = UIView()
    @objc public let name = UILabel()
    @objc public let time = UILabel()
    @objc public let content = UILabel()
    @objc public let replyView = UIView()//回复
    @objc public var viewWidth:CGFloat = 0
    @objc public var viewHeight:CGFloat = 0
    @objc public var model:TFQuestionData?
    {
        didSet{
            if(model != nil)
            {
                self.modelUI(model!)
            }
            
        }
    }
    
    func modelUI(_ model:TFQuestionData)
    {
        self.name.isHidden = false
        self.content.isHidden = false
        self.time.isHidden = false

        //其它人的名字
        var nickname:NSString = model.nickname as NSString

        //时间
        let currentDateStr = TalkfunSharedTools.getTimestamp(mStr: model.time as NSString)
            
        let timeTextColor = UIColor(red: 126/255.0, green: 133/255.0, blue: 151/255.0, alpha: 1)

        
        self.time.attributedText = self.getOthersNameString(currentDateStr as String, timeTextColor: timeTextColor)

        if (nickname.length > 6) {
      
            nickname =  nickname.substring(with:NSRange(location: 0, length: 6)) as NSString
            nickname =   (nickname as String) + "..." as NSString

        }
        let size: CGSize = nickname.size(withAttributes: [NSAttributedString.Key.font: UIFont.PingFang(size: 14)])
        
        let timeW:CGFloat = 48
        var  nameW:CGFloat = size.width ;
        if (size.width > self.viewWidth - timeW - 14 - 14) {
            nameW = self.viewWidth - 14 - 14 - timeW;
        }
        
        self.name.attributedText = self.getOthersNameString(nickname as String, timeTextColor: timeTextColor)

        let othersNameX:CGFloat = 55
        let othersNameY:CGFloat = 15
        let nameH:CGFloat = 20
        self.name.frame = CGRect(x: othersNameX, y: othersNameY, width: nameW, height: nameH)
     
        let timeH:CGFloat = nameH
        let timeX:CGFloat = othersNameX+nameW+15
        let timeY:CGFloat = othersNameY
        self.time.frame = CGRect(x: timeX, y: timeY, width: timeW, height: timeH)
       
       let String = TalkfunSharedTools.getAttributedText(model.content, fontSize: UIFont.PingFang(size: 14), foregroundColor: UIColor.white)
        self.content.attributedText = String
        
        let size11 = TalkfunSharedTools.getSizeLabel(labelString: String, labelWidth: self.viewWidth - 55 - 15, labelfont: UIFont.PingFang(size: 14))
        
        
        
        self.content.frame = CGRect(x: 55, y: 36.5, width: self.viewWidth - 55 - 15, height: size11.height)
  
        self.name.textAlignment    = .left
        self.content.textAlignment =  .left
        if self.replyView.subviews.count>0{
          self.replyView.subviews.forEach({$0.removeFromSuperview()})
        }
        
        
        if (model.answer.count>0) {
              
            let answer = model.answer as NSArray
            var contentHight:CGFloat = 0
            var vcY:CGFloat = 0
            for i in 0..<answer.count {
          
                let dict1 =  answer[i] as! TFQuestionAnswer
                
               
                let string2 = TalkfunSharedTools.getAttributedText(dict1.content, fontSize: UIFont.PingFang(size: 14), foregroundColor: UIColor.white)
                let size2 = TalkfunSharedTools.getSizeLabel(labelString: string2, labelWidth: self.viewWidth - 85 - 15, labelfont: UIFont.PingFang(size: 14))
               
                
                
                let vc   = UIView()
                let vcH:CGFloat  = (15+5+size2.height)
                contentHight = contentHight+((CGFloat(answer.count)-1)*5)
                vc.frame = CGRect(x: 0, y: vcY, width: self.replyView.frame.size.width, height: vcH)
                
                
                          vcY     = vcH + 5 + contentHight
                    contentHight  = contentHight + vcH
                self.replyView.addSubview(vc)
                
                //剪头不
                let ReplyImage = UIImageView()
                ReplyImage.frame = CGRect(x: 0, y: 4, width: 17, height: 14)
                ReplyImage.image = UIImage.init(named: "HTLive.bundle/Reply1vN")
                vc.addSubview(ReplyImage)
                
                
                  //回复的老师的头像
                let avatarImage1 = UIImageView()
                avatarImage1.frame = CGRect(x: 14+7.5, y: 0, width: 25, height: 25)
                avatarImage1.layer.cornerRadius =  avatarImage1.bounds.size.width / 2
                let avatarImageStr1 = dict1.avatar
                avatarImage1.sd_setImage(with: NSURL.init(string: avatarImageStr1) as URL?)
                vc.addSubview(avatarImage1)
                
                
                //其它人的名字
                let nameLaberl = UILabel()
                nameLaberl.backgroundColor = UIColor.clear
                nameLaberl.numberOfLines = 0;
                vc.addSubview(nameLaberl)
         
                //时间
                let timeLaberl = UILabel()
                timeLaberl.backgroundColor = UIColor.clear
                timeLaberl.numberOfLines = 0;
                vc.addSubview(timeLaberl)
                //聊天内容
                let contentLaberl = UILabel()
                //防止自动换行
                contentLaberl.lineBreakMode = .byCharWrapping
                contentLaberl.font = UIFont.PingFang(size: 14)

                contentLaberl.numberOfLines = 0
                vc.addSubview(contentLaberl)
               
               
                //其它人的名字
                var nickname = dict1.nickname as NSString
                //时间

                
                timeLaberl.attributedText = self.getOthersNameString(TalkfunSharedTools.getTimestamp(mStr: dict1.time as NSString) as String, timeTextColor: timeTextColor)
   
                if (nickname.length > 6) {
      
                    nickname =  nickname.substring(with:NSRange(location: 0, length: 6)) as NSString
                    nickname =   (nickname as String) + "..." as NSString
                }
           
                let size: CGSize = nickname.size(withAttributes: [NSAttributedString.Key.font: UIFont.PingFang(size: 14)])
                
                let timeW:CGFloat = 48
                var nameW:CGFloat = size.width
                if (size.width>self.viewWidth - timeW - 85 - 15) {
                    nameW = self.viewWidth - 85 - 15 - timeW
                }
                
                nameLaberl.attributedText = self.getOthersNameString(nickname as String, timeTextColor: timeTextColor)

                let othersNameX:CGFloat = avatarImage1.frame.maxX+5
                let othersNameY:CGFloat = 0
                let nameH:CGFloat = 20
                nameLaberl.frame = CGRect(x: othersNameX, y: othersNameY, width: nameW, height: nameH)
            
                
                let timeH:CGFloat = nameH;
                let timeX:CGFloat = othersNameX+nameW+15;
                let timeY:CGFloat = othersNameY;
                timeLaberl.frame = CGRect(x: timeX, y: timeY, width: timeW, height: timeH)
               
                
                let string = self.getOthersNameString(dict1.content, timeTextColor: UIColor.white)
                
                contentLaberl.attributedText = string
                
                
                let size11 =  TalkfunSharedTools.getSizeLabel(labelString: string, labelWidth: self.viewWidth-85-15, labelfont: UIFont.PingFang(size: 14))
               
                
                contentLaberl.frame = CGRect(x: othersNameX, y: nameLaberl.frame.maxY+3, width: self.viewWidth-85-15, height: size11.height)
                
                nameLaberl.textAlignment    = .left
                contentLaberl.textAlignment =  .left
                
            }
            self.replyView.frame = CGRect(x: 29, y: self.content.frame.maxY+20, width: self.viewWidth-29, height: contentHight)
           
        }else{
            self.replyView.frame = CGRect(x: 0, y: 0, width: 0, height: 0)
        }
        
    }

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.questionerAvatar.layer.borderColor = UIColor(red: 112/255.0, green: 112/255.0, blue: 112/255.0, alpha: 1).cgColor
        self.questionerAvatar.layer.borderWidth = 0.5
        self.questionerAvatar.layer.masksToBounds  = true
        self.addSubview(self.questionerAvatar)
        
        self.underline .backgroundColor = UIColor(red: 53/255.0, green: 56/255.0, blue: 59/255.0, alpha: 1)
        self.addSubview(self.underline)

        //其它人的名字
        self.name.backgroundColor = UIColor.clear
        self.name.numberOfLines = 0
        self.addSubview(self.name)

        //时间
        self.time.backgroundColor = UIColor.clear
        self.time.numberOfLines = 0
        self.addSubview(self.time)
        
        //聊天内容
        self.content.isHidden = true
        //防止自动换行
        self.content.lineBreakMode = .byCharWrapping
        self.content .font = UIFont.PingFang(size: 14)
        self.content.backgroundColor = UIColor.clear
        self.content.numberOfLines = 0;
        self.addSubview(self.content)
    
        self.addSubview(self.replyView)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        self.questionerAvatar.frame = CGRect(x: 15, y: 20.5, width: 32, height: 32)

        self.questionerAvatar.layer.cornerRadius =  self.questionerAvatar.bounds.size.width / 2

        self.underline.frame = CGRect(x: 15, y: self.frame.size.height-1, width: self.frame.size.width-30, height: 1)
      
    }
    func getOthersNameString(_ name:String,timeTextColor:UIColor)->NSMutableAttributedString
    {
        return NSMutableAttributedString.getAttributed(string: name , font: UIFont.PingFang(size: 14), foregroundColor: timeTextColor);
    }
}
